package com.lzm.ydpt;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.byt.framlib.commonutils.SharePerUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.util.EasyUtils;
import com.lzm.ydpt.module.common.WelActivity;
import com.lzmpt.imchat.DemoHelper;
import com.lzmpt.imchat.conference.ConferenceActivity;
import com.lzmpt.imchat.ui.BaseActivity;
import com.lzmpt.imchat.ui.VideoCallActivity;
import com.lzmpt.imchat.ui.VoiceCallActivity;

import io.reactivex.annotations.Nullable;

public class WelcomeActivity extends BaseActivity {


    private static final String TAG = "WelcomeActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DemoHelper.getInstance().initHandler(this.getMainLooper());

        String token = SharePerUtils.getSharedString("token");
        //未登录
        if (TextUtils.isEmpty(token) ) {
            goToLogin();
        } else {
            imIsLogin();

        }


    }

    private void imIsLogin() {
        new Thread(new Runnable() {
            public void run() {
                Log.e(TAG, "run: "+DemoHelper.getInstance().isLoggedIn() );
                if (DemoHelper.getInstance().isLoggedIn())
                {

                    EMClient.getInstance().chatManager().loadAllConversations();
                    EMClient.getInstance().groupManager().loadAllGroups();

                    String topActivityName = EasyUtils.getTopActivityName(EMClient.getInstance().getContext());
                    if (topActivityName != null && (topActivityName.equals(VideoCallActivity.class.getName()) || topActivityName.equals(VoiceCallActivity.class.getName()) || topActivityName.equals(ConferenceActivity.class.getName()))) {
                        // nop
                        // avoid main screen overlap Calling Activity
                        goToLogin();
                    } else {
                        //enter main screen
                        goToMain();
                    }
                }else {

                    goToLogin();
                }
            }
        }).start();
    }

    private void goToLogin() {

        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
        finish();

    }

    private void goToMain() {

        startActivity(new Intent(WelcomeActivity.this, WelActivity.class));
        finish();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

}
